#include <QtGui>
#include "topoFrame.h"
#include "topoScene.h"
#include <iostream>
using namespace std;
#define DBG(str) // cout << __PRETTY_FUNCTION__ << " - " << str << endl; // qDebug("%s - %s",__PRETTY_FUNCTION__, str)


TopoFrame::TopoFrame(QRectF rect, int nID,
                     QGraphicsItem *parent)
    : QGraphicsRectItem(rect, parent), m_bRemoved(false), m_nCursorType(0), m_bInDragArea(false), m_bInDragMode(false),
      m_nFrameID(-1)
{
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemIsMovable, true);
    m_CursorShape = cursor ().shape ();
    setRect(rect);
    //setMouseTracking(true);
    setAcceptHoverEvents(true);
    /*m_gTextItem = new QGraphicsTextItem("...");
    m_gTextItem->setDefaultTextColor(Qt::darkCyan);
    m_gTextItem ->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable );
    m_gTextItem ->setTextInteractionFlags(Qt::TextEditorInteraction);
    QFont font;
    font.setFamily("SimSun");
    font.setPointSize(10);
    m_gTextItem->setFont(font);
    m_gTextItem->setPos(-10, 22);
    m_gTextItem->setPlainText(text);
    m_gTextItem->setVisible(true);*/
    m_nFrameID = nID;
    //m_text = text;
}

QRectF TopoFrame::boundingRect() const
{
    qreal extra = 1;
    return  rect().normalized() .adjusted(-extra, -extra, extra, extra);
}


void TopoFrame::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                      QWidget *widget)
{
    if (m_bRemoved)
    {
        return;
    }
    if (isSelected())
    {
        painter->setPen(QPen(mySelectedColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
        //painter->setBrush(mySelectedColor);
    }
    else
    {
        painter->setPen(QPen(Qt::gray, 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin));
        //painter->setBrush(QColor(Qt::gray));
    }

    painter->drawRect (rect ());
    //qDebug() << "paint:" << rect().left() << rect ().right() << rect().top() << rect().bottom( );
    //QPoint pos(2, 2);
    //painter->drawText ( pos, m_text );
    //qDebug("Line Paint is done....................");
    //m_gTextItem->paint(painter,option,widget);
}

void TopoFrame::hoverMoveEvent ( QGraphicsSceneHoverEvent *event )
{
    qreal extra = (pen().width() + 4) / 2.0;
    int nSetCursorType = 0;

    QRectF rect1 = QRectF(rect ().topLeft (), rect ().topRight ()).normalized().adjusted(-extra, -extra, extra, extra);
    QRectF rect2 = QRectF(rect ().bottomLeft (), rect ().bottomRight ()).normalized().adjusted(-extra, -extra, extra,
                   extra);
    QRectF rect3 = QRectF(rect ().topLeft (), rect ().bottomLeft ()).normalized().adjusted(-extra, -extra, extra, extra);
    QRectF rect4 = QRectF(rect ().topRight (), rect ().bottomRight ()).normalized().adjusted(-extra, -extra, extra, extra);
    QRectF rect5 = QRectF(rect ().topLeft (), rect ().topLeft ()).normalized().adjusted(-extra, -extra, extra, extra);
    QRectF rect6 = QRectF(rect ().topRight (), rect ().topRight ()).normalized().adjusted(-extra, -extra, extra, extra);
    QRectF rect7 = QRectF(rect ().bottomLeft (), rect ().bottomLeft ()).normalized().adjusted(-extra, -extra, extra, extra);
    QRectF rect8 = QRectF(rect ().bottomRight (), rect ().bottomRight ()).normalized().adjusted(-extra, -extra, extra,
                   extra);
    if (rect5.contains(event->pos ()) )
    {
        nSetCursorType = 1;
    }
    else if (rect6.contains(event->pos ()))
    {
        nSetCursorType = 2;
    }
    else if (rect7.contains(event->pos ()))
    {
        nSetCursorType = 3;
    }
    else if (rect8.contains(event->pos ()))
    {
        nSetCursorType = 4;
    }
    else if (rect1.contains(event->pos ()) )
    {
        nSetCursorType = 5;
    }
    else if (rect2.contains(event->pos ()))
    {
        nSetCursorType = 6;
    }
    else if (rect3.contains(event->pos ()))
    {
        nSetCursorType = 7;
    }
    else if (rect4.contains(event->pos ()))
    {
        nSetCursorType = 8;
    }
    else if (rect().contains(event->pos ()))
    {
        nSetCursorType = 9;
    }

    //qDebug( "hoverMoveEvent %d.%d...........", nSetCursorType, m_nCursorType);
    if (nSetCursorType != m_nCursorType)
    {
        //qDebug( "hoverMoveEvent set cursor %s %d..............",
        //        (nSetCursorType != 0) ? "split" : "normal" , nSetCursorType);
        switch (nSetCursorType)
        {
        case 1:
        case 4:
            setCursor(Qt::SizeFDiagCursor);
            break;
        case 2:
        case 3:
            setCursor(Qt::SizeBDiagCursor);
            break;
        case 5:
        case 6:
            setCursor(Qt::SizeVerCursor);
            break;
        case 7:
        case 8:
            setCursor(Qt::SizeHorCursor);
            break;
        case 9:
            setCursor(Qt::SizeAllCursor);
            break;
        default:
            unsetCursor ();//(m_CursorShape);
            break;
        }
        m_nCursorType = nSetCursorType;
    }
    m_bInDragArea = (nSetCursorType > 0 && nSetCursorType < 9);
    QGraphicsRectItem::hoverMoveEvent(event);
}

void TopoFrame::hoverLeaveEvent ( QGraphicsSceneHoverEvent *event )
{
    if (m_nCursorType != 0)
    {
        unsetCursor();
        m_nCursorType = 0;
        m_bInDragArea = false;
        qDebug( "hoverLeaveEvent set cursor normal" );
    }
    QGraphicsRectItem::hoverLeaveEvent(event);
}

void TopoFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (m_bInDragMode)
    {
        QRectF curRect = rect();
        int nYOffset = event->scenePos().y() - m_OrgPoint.y();
        int nXOffset = event->scenePos().x() - m_OrgPoint.x();
        switch (m_nCursorType)
        {
        case 1:
            curRect.setTop(nYOffset + m_OrgRect.top());
            curRect.setLeft(nXOffset + m_OrgRect.left ());
            setRect (curRect);
            break;
        case 2:
            curRect.setTop(nYOffset + m_OrgRect.top());
            curRect.setRight(nXOffset + m_OrgRect.right ());
            setRect (curRect);
            break;
        case 3:
            curRect.setLeft(nXOffset + m_OrgRect.left ());
            curRect.setBottom(nYOffset + m_OrgRect.bottom ());
            setRect (curRect);
            break;
        case 4:
            curRect.setRight(nXOffset + m_OrgRect.right ());
            curRect.setBottom(nYOffset + m_OrgRect.bottom ());
            setRect (curRect);
            break;
        case 5:
            curRect.setTop(nYOffset + m_OrgRect.top());
            setRect (curRect);
            break;
        case 6:
            curRect.setBottom(nYOffset + m_OrgRect.bottom ());
            setRect (curRect);
            break;
        case 7:
            curRect.setLeft(nXOffset + m_OrgRect.left ());
            setRect (curRect);
            break;
        case 8:
            curRect.setRight(nXOffset + m_OrgRect.right ());
            setRect (curRect);
            break;
        default:
            break;
        }
        //qDebug() << "mouseMoveEvent:" << rect().left() << rect ().right() << rect().top() << rect().bottom( ) << nXOffset <<
        //         nYOffset << event->pos().x() << event->pos().y();
        //m_OrgPoint = event->pos();
    }
    else
    {
        QGraphicsRectItem::mouseMoveEvent(event);
    }
}

void TopoFrame::mousePressEvent ( QGraphicsSceneMouseEvent *event )
{
    if (m_bInDragArea)
    {
        m_bInDragMode = true;
        m_OrgPoint = event->scenePos();
        m_OrgRect = rect();
        //qDebug() << "mousePressEvent:" << event->pos().x() << event->pos().y();
    }
    QGraphicsRectItem::mousePressEvent(event);
}

void TopoFrame::mouseReleaseEvent ( QGraphicsSceneMouseEvent *event )
{
    if (m_bInDragMode)
    {
        QRectF curRect = rect();
        switch (m_nCursorType)
        {
        case 1:
            curRect.setTop(event->scenePos().y() - m_OrgPoint.y() + m_OrgRect.top());
            setRect (curRect);
            break;
        case 2:
            curRect.setBottom(event->scenePos().y() - m_OrgPoint.y() + m_OrgRect.bottom ());
            setRect (curRect);
            break;
        case 3:
            curRect.setLeft(event->scenePos().x() - m_OrgPoint.x() + m_OrgRect.left ());
            setRect (curRect);
            break;
        case 4:
            curRect.setRight(event->scenePos().x() - m_OrgPoint.x() + m_OrgRect.right ());
            setRect (curRect);
            break;
        default:
            break;
        }
        unsetCursor();
        m_nCursorType = 0;
    }
    m_bInDragMode = false;
    QGraphicsRectItem::mouseReleaseEvent(event);
}
